#ifndef EVENTMANAGER_H
#define EVENTMANAGER_H

#include <stdint.h>
#include <time.h>
#include <common/array.h>

class ParamManager;
class Route;
class Watcher;
class Queue;

enum {
	evt_ready = 1, evt_failure, evt_critical_failure, 
	evt_bad_coord, evt_good_coord,
	evt_engine_stopped, evt_engine_warmed, 
	evt_launch,
    evt_vrotate, evt_vretract,
	evt_vtakeoffstop,
	evt_lift_off, evt_initial_alt, 
	evt_fly_to, evt_route_end,
	evt_glidepath, evt_glidepath_end,
	evt_directalt, evt_directalt_lost, evt_minimum_baro, evt_flare_alt, evt_touchdown,
	evt_parachute_alt,
	evt_timer,
	evt_param_error,
	evt_datalink, evt_rc, evt_datalink_lost, evt_rc_lost,
	evt_flaps_landing,
	evt_throttle_on, evt_throttle_off, evt_ignition_off,
	evt_moving, evt_stopped,
	evt_alarm_rc,
	evt_cmd_manual, evt_cmd_auto, 
	evt_cmd_engine_start, evt_cmd_takeoff, evt_cmd_landing, 
	evt_cmd_abort, evt_cmd_emergency
};

class EventManager {
public:
	EventManager(ParamManager *parammgr, Route *route);
	~EventManager();

	void attach(Queue *queue);
	void check(const Array<int> &events);
	void post(int evt);
	void setTimer(uint32_t period);
	
private:
	bool isFired(int evt);
	bool isFailure();
	bool isCriticalFailure();
	bool isGoodCoord();
	bool isEngineRunning();
	bool isEngineCold();	
	bool isLaunch();
	bool isVrotate();
	bool isVretract();
	bool isVtakeoffStop();
	bool isInitialAlt();
	bool isMinimumBaro();
	bool isOnPattern();
	bool isOnGlidePath();
	bool isDirectAlt();
	bool isFlareAlt();
	bool isOnGround();
	bool isParachuteAlt();
	bool isTimeout();
	bool isParamError();
	bool isThrottleOn();
	bool isIgnitionOn();	
	bool isFlapsLanding();	
	bool isManualRC();
	bool isRCLost();
	bool isDatalinkLost();
	bool isStopped();
	bool isAlarmRC();

	ParamManager *parammgr_;
	Route *route_;
	Watcher *watcher_;
	
	bool timerActive_;
	timespec timerValue_;
	
	Queue *queue_;
};

#endif
